<?php

namespace App\Http\Controllers\Video;

use App\Models\Video;
use Illuminate\Support\Facades\Log;
use App\Utils\Transocding\DefaultAcsClient;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use APP\Utils\Transcoding\Config;
use App\Utils\Transocding\Mts\SubmitJobsRequest;

use App\Utils\Transocding\Profile\DefaultProfile;
use Illuminate\Support\Facades\DB;

class TranscodingController extends Controller
{
	public $access_key_id = "LTAIJPXZ5xRBpkJJ"; # AccessKey ID
	public $access_key_secret = "cRUMuvKyH3TJ2010f9bo9P2CyZpgkI"; # AccessKey Secret
	public $mps_region_id = "cn-beijing"; 	#  Region ID
	public $pipeline_id = 'a17107261846453ebfd68499e4d76c60';
	public $template_id = "S00000001-200030";//mp4---高清视频转码
	public $oss_location = 'oss-cn-beijing';
	public $oss_bucket = 'saipujianshenbbs';
	public $preUrl = "https://video.saipubbs.com/";
    /**
     * 转码
     */
    public function index(Request $a)
    {
		$data = $a->input();
		$url = trim($data['url'], "'");
		$id = trim($data['id'], "'");
		
		//获取原地址
		$arr = explode("/",$url);
        $newArr = array_slice($arr,3);
        $str = "";
        foreach($newArr as $v){
            $str.= "/".$v;
        }
        $newStr = substr($str,1);
		//获取目录地址
		$last_name = array_pop($newArr);
		$directory = "";
		foreach($newArr as $c){
			$directory .= "/".$c;
		}
		
		$newDirectory = substr($directory,1);
		
		//开始转码
        $oss_input_object = $newStr;
		$oss_output_object = $newDirectory."/".date("Ymd")."-".$last_name;
		
		# 创建DefaultAcsClient实例并初始化		
		
		$clientProfile = DefaultProfile::getProfile(
			$this->mps_region_id,                   
			$this->access_key_id,                   
			$this->access_key_secret                
		);
		$client = new DefaultAcsClient($clientProfile);
		# 创建API请求并设置参数
		$request = new SubmitJobsRequest();
		
		$request->setAcceptFormat('JSON');
		# Input
		$input = array('Location' => $this->oss_location,
					   'Bucket' => $this->oss_bucket,
					   'Object' => urlencode($oss_input_object));
		$request->setInput(json_encode($input));
		# Output
		$output = array('OutputObject' => urlencode($oss_output_object));
		# Ouput->Container
		$output['Container'] = array('Format' => 'mp4');
		# Ouput->Video
		$output['Video'] = array('Codec' =>'H.264',
								 'Bitrate' => 1500,
								 'Width' => 1280,
								 'Fps' => 25);
		# Ouput->Audio
		$output['Audio'] = array('Codec' => 'AAC',
                         'Bitrate' => 128,
                         'Channels' => 2,
                         'Samplerate' => 44100);
		# Ouput->TemplateId
		$output['TemplateId'] = $this->template_id;
		$outputs = array($output);
		$request->setOUtputs(json_encode($outputs));
		$request->setOutputBucket($this->oss_bucket);
		$request->setOutputLocation($this->oss_location);
		# PipelineId
		$request->setPipelineId($this->pipeline_id);
		
		try{
			$response = $client->getAcsResponse($request);
			
			//print 'RequestId is:' . $response->{'RequestId'} . "\n";
			
			if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
				
			//print 'JobId is:' .
					//$response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
					$video = new Video();
					//$re = $video->deleteVideo($newStr);
					$sqlData['url'] = $this->preUrl.$oss_output_object;
					$sqlData['is_trans'] = 1;
					$sqlData['old_url'] = $url;
					$re = $video->updateData($id,$sqlData);
					echo "<script>window.location.href=document.referrer;</script>";
			} else {
				
				print 'SubmitJobs Failed code:' .
					   $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
					   ' message:' .
					   $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
					   echo "<script>window.location.href=document.referrer;</script>";
			}
		}catch(ServerException $e){
			print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
			echo "<script>window.location.href=document.referrer;</script>";
				
		}catch(ClientException $e){
			print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
			echo "<script>window.location.href=document.referrer;</script>";
				
		}
    }


    
}

















